const http = require('http')
const fs = require('fs')
let persons = fs.readFileSync(__dirname + "/persons.json", "utf-8")
persons = JSON.parse(persons)
console.log(persons);
let user = {
    username: "admin1",
    password: "12345"
}
let server = http.createServer((req, res) => {
    res.writeHead(200, {
        "content-type": "text/html;charset=utf-8"
    });
    if (req.url == "/login") {
        persons.find(v => v.username == user.username && v.password == user.password) ?
            res.end("登录成功") :
            res.end("登录失败")
    } else if (req.url == "/reg") {
        let flag = persons.findIndex(v => v.username == user.username)
        if (flag == -1) {
            res.end("可以注册")
            persons.push(user)
            let data=JSON.stringify(persons)
            fs.writeFileSync(__dirname+"/persons.json",data)
        }else{
            res.end('用户名已经被占用')
        }
    }
}) 
server.listen(3000, "127.0.0.1", () => {
    console.log("启动成功");
})